#include <stdio.h>
typedef struct node {
	int val;	//用来记录数字
	struct node* p_next;	//用来指向后一个节点
} node;	//代表线性链式物理结构中节点的结构体
int main() {
	node node1 = {10}, node2 = {20}, node3 = {30};
	node node4 = {25};
	node head = {0}/*无效头结点*/, tail = {0}/*无效尾节点*/;
	node* p_tmp = NULL;
	head.p_next = &node1;
	node1.p_next = &node2;
	node2.p_next = &node3;
	node3.p_next = &tail;
	//把node4插入到线性链式物理结构中，插入后保持所有数字仍然是由小到大排列
	for(p_tmp = &head; p_tmp != &tail; p_tmp = p_tmp->p_next) {
		node* p_first = p_tmp;
		node* p_mid = p_first->p_next;
		node* p_last = p_mid->p_next;
		if(p_mid == &tail/*p_mid指针指向尾节点*/ ||  p_mid->val > node4.val/*p_mid指针第一次比node4节点里的数字大*/) {
			p_first->p_next = &node4;
			node4.p_next = p_mid;
			break;
		}
	}
	//把20所在的节点从链式物理结构里删除
	for(p_tmp = &head; p_tmp != &tail; p_tmp = p_tmp->p_next) {
		node* p_first = p_tmp;
		node* p_mid = p_first->p_next;
		node* p_last = p_mid->p_next;
		if(p_mid->val == 20) {	//p_mid指针指向的节点里就是20
			p_first->p_next = p_last;
			break;
		}
	}
	//指针循环变量p_tmp从头节点开始向后依次指向每个节点，
	//直到最后一个有效节点为止
	for(p_tmp = &head; p_tmp != &tail; p_tmp = p_tmp->p_next) {
		//以下三个指针永远指向三个相邻的节点，
		//其中p_first指向其中最前面的节点，
		//p_last指针指向其中最后面的节点，
		//p_mid指针从第一个有效节点一直指向到尾节点
		node* p_first = p_tmp;
		node* p_mid = p_first->p_next;
		node* p_last = p_mid->p_next;
		if(p_mid != &tail) {
			printf("%d ",p_mid->val);
		}
	}
	printf("\n");
	return 0;
}
